package com.jcl.cloud.common.service.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

public class PageOps {


    private static Logger logger = LoggerFactory.getLogger(PageOps.class);


    //https://rd5.zhaopin.com/custom/searchv2/result
    public static String getSearchv2Result(String Cookie,HttpClientContext context){
        String result = "";
        String url = "https://rd5.zhaopin.com/custom/searchv2/result";
        int times = 0;
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpGet httpGet = null;
        try {
            httpGet = new HttpGet(url);

            httpGet.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpGet.setHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");

            httpGet.setHeader("Host", "rd5.zhaopin.com");
            httpGet.setHeader("Cookie", Cookie);



            response = HttpClientUtil.getHttpClient().execute(httpGet, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：

        } catch (Exception e) {
            logger.error(String.format("getSearchv2Result 发生异常：%s",e),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpGet.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;
    }





    //https://rdapi.zhaopin.com/custom/search/resumeListV2?_=1531792170230
    public static String getResumeListV2(Map<String,String> map,HttpClientContext context){
        String result = "";
        String url = "https://rdapi.zhaopin.com/custom/search/resumeListV2?_=" + new Date().getTime();
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        try {
            httpPost = new HttpPost(url);
            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpPost.setHeader("Content-Type", "text/plain");
            httpPost.setHeader("Referer", "https://rd5.zhaopin.com/custom/searchv2/result");
            httpPost.setHeader("zp-route-meta", map.get("zp_route_meta"));

            httpPost.setHeader("Origin", "https://rd5.zhaopin.com");
            httpPost.setHeader("Host", "rdapi.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));


            StringEntity se = new StringEntity(map.get("inJson"));
            httpPost.setEntity(se);


           response = HttpClientUtil.getHttpClient().execute(httpPost, context);
            resEntity = response.getEntity();
           result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getResumeListV2 发生异常：%s",e.getMessage()),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }




    //https://rdapi.zhaopin.com/custom/search/getSummary?_=1531792171206
    public static String getSummary(Map<String,String> map,HttpClientContext context){
        String result = "";
        String url = "https://rdapi.zhaopin.com/custom/search/getSummary?_=" + new Date().getTime();
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        try {
            httpPost = new HttpPost(url);
            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpPost.setHeader("Content-Type", "text/plain");
            httpPost.setHeader("Referer", "https://rd5.zhaopin.com/custom/searchv2/result");
            httpPost.setHeader("zp-route-meta", map.get("zp_route_meta"));

            httpPost.setHeader("Origin", "https://rd5.zhaopin.com");
            httpPost.setHeader("Host", "rdapi.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));


            StringEntity se = new StringEntity(map.get("inJson"));
            httpPost.setEntity(se);


            response = HttpClientUtil.getHttpClient().execute(httpPost, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getSummary 发生异常：%s",e.getMessage()),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }






    //https://rdapi.zhaopin.com/custom/search/markRead?_=1531792694770
    public static String doMarkRead(Map<String,String> map,HttpClientContext context){
        HttpEntity resEntity = null;
        String result = "";
        String url = "https://rdapi.zhaopin.com/custom/search/markRead?_=" + new Date().getTime();
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        try {
            httpPost = new HttpPost(url);
            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpPost.setHeader("Content-Type", "text/plain");
            httpPost.setHeader("Referer", "https://rd5.zhaopin.com/custom/searchv2/result");
            httpPost.setHeader("zp-route-meta", map.get("zp_route_meta"));

            httpPost.setHeader("Origin", "https://rd5.zhaopin.com");
            httpPost.setHeader("Host", "rdapi.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));


            StringEntity se = new StringEntity(map.get("inJson"));
            httpPost.setEntity(se);


            response = HttpClientUtil.getHttpClient().execute(httpPost, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("doMarkRead 发生异常：%s",e.getMessage()),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }






    //https://rdapi.zhaopin.com/rd/resume/list/counts?_=1528956900678
    public static String getListCounts(Map<String, String> map, HttpClientContext context, StringEntity se) {

        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        try {
            //https://rd5.zhaopin.com/api/rd/resume/list/counts?_=1537175553628
            httpPost = new HttpPost("https://rd5.zhaopin.com/api/rd/resume/list/counts?_=" + new Date().getTime());
            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
           // httpPost.setHeader("Content-Type", "text/plain");
            httpPost.setHeader("Referer", "https://rd5.zhaopin.com/resume/apply");
            httpPost.setHeader("zp-route-meta", map.get("zp_route_meta"));

            //httpPost.setHeader("Origin", "https://rd5.zhaopin.com");
            //httpPost.setHeader("Connection", "keep-alive");
            httpPost.setHeader("Host", "rdapi.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));

            httpPost.setHeader("host", "rd5.zhaopin.com");

            httpPost.setEntity(se);


            response = HttpClientUtil.getHttpClient().execute(httpPost, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getListCounts 发生异常：%s",e));
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return result;
    }



    public  static String getResumeList(Map<String, String> map, HttpClientContext context, StringEntity se){


        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        try {

            //https://rd5.zhaopin.com/api/rd/resume/list?_=1537240104447
            httpPost = new HttpPost("https://rd5.zhaopin.com/api/rd/resume/list?_="+new Date().getTime());

            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpPost.setHeader("Content-Type", "text/plain");
            httpPost.setHeader("X-Requested-With", "XMLHttpRequest");


            httpPost.setHeader("Referer", "https://rd5.zhaopin.com/resume/apply");
            httpPost.setHeader("zp-route-meta", map.get("zp_route_meta"));

            httpPost.setHeader("Origin", "https://rd5.zhaopin.com");
            httpPost.setHeader("Connection", "keep-alive");
            httpPost.setHeader("Host", "rd5.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));

            httpPost.setEntity(se);


            response = HttpClientUtil.getHttpClient().execute(httpPost, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getResumeList 发生异常：%s",e));
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;
    }


    public static String getResumeDetail(Map<String, String> map, HttpClientContext context){
        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpGet httpGet = null;
        try {
            httpGet = new HttpGet(map.get("url"));

            httpGet.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            //httpGet.setHeader("Content-Type", "text/plain");
            httpGet.setHeader("X-Requested-With", "XMLHttpRequest");
             /* String Referer = map.get("Referer");
              if(StringUtils.isBlank(Referer)){
                  httpGet.setHeader("Referer", "https://rd5.zhaopin.com/resume/apply");
              }else {
                  httpGet.setHeader("Referer", Referer);
              }
*/

            httpGet.setHeader("Referer", map.get("Referer"));
            httpGet.setHeader("zp-route-meta", map.get("zp_route_meta"));

            //httpGet.setHeader("Origin", "https://rd5.zhaopin.com");
            httpGet.setHeader("Connection", "keep-alive");
            httpGet.setHeader("Host", "rd5.zhaopin.com");
            httpGet.setHeader("Cookie", map.get("Cookie"));

            response = HttpClientUtil.getHttpClient().execute(httpGet, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getResumeDetail 发生异常：%s",e));
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpGet.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;
    }



    public static String getJobListByEmail(Map<String,String> map,HttpClientContext context){
        String url = "https://rdapi.zhaopin.com/rd/job/list?_="+new Date().getTime()
                +"&jobState=publish&page=1&pageSize=100&email="+map.get("email").replaceAll("@","%40")
                +"&follow=false";

        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpGet httpGet = null;
        try {
             httpGet = new HttpGet(url);

            httpGet.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
            httpGet.setHeader("Content-Type", "text/plain");
            httpGet.setHeader("Referer", "https://rd5.zhaopin.com/job/manage");
            httpGet.setHeader("zp-route-meta", map.get("zp_route_meta"));

            httpGet.setHeader("Origin", "https://rd5.zhaopin.com");
            httpGet.setHeader("Connection", "keep-alive");
            httpGet.setHeader("Host", "rdapi.zhaopin.com");
            httpGet.setHeader("Cookie", map.get("Cookie"));


            response = HttpClientUtil.getHttpClient().execute(httpGet, context);
            resEntity = response.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：
        } catch (Exception e) {
            logger.error(String.format("getResumeDetail 发生异常：%s",e));
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpGet.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;
    }








    /****************************************购买简历相关接口-start********************************************/
    //https://rd5.zhaopin.com/api/rd/resume/folders?_=1539136532374
    public static String getFolders(HttpClientContext context,Map<String,String> map){


        String url = "https://rd5.zhaopin.com/api/rd/resume/folders?_="+new Date().getTime();


        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpGet httpGet = null;
        int times = 0;
        try {
            httpGet = new HttpGet(url);

            httpGet.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");

            httpGet.setHeader("Referer", map.get("Referer"));
            httpGet.setHeader("Host", "rd5.zhaopin.com");
            httpGet.setHeader("Cookie", map.get("Cookie"));

            CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

            do {
                times++;
                try {
                    response = httpClient.execute(httpGet, context);
                    resEntity = response.getEntity();
                    result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：

                    if(StringUtils.isNotBlank(result)){
                        JSONObject resultJson = JSON.parseObject(result);
                        String code  = resultJson.getString("code");
                        String data = resultJson.getString("data");

                        if("0".equalsIgnoreCase(code) && StringUtils.isNotBlank(data)){
                            break;
                        }
                    }

                } catch (Exception e) {
                    logger.error("getFolders 调用接口发生异常：" + e.getMessage(),e);
                }

                if(times > 6){
                    break;
                }
            } while (true);


        } catch (Exception e) {
            logger.error(String.format("getFolders 发生异常：%s",e),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpGet.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;
    }




    //https://rd5.zhaopin.com/api/rd/resume/download?_=1539136606682
    public static String  doPostToResumeDownload(HttpClientContext context,Map<String,String> map,StringEntity se){

        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpPost httpPost = null;
        int times = 0;
        try {

            httpPost = new HttpPost("https://rd5.zhaopin.com/api/rd/resume/download?_="+new Date().getTime());

            httpPost.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpPost.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpPost.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");



            httpPost.setHeader("Referer", map.get("Referer"));


            httpPost.setHeader("Origin", "https://rd5.zhaopin.com");

            httpPost.setHeader("Host", "rd5.zhaopin.com");
            httpPost.setHeader("Cookie", map.get("Cookie"));

            httpPost.setEntity(se);

            CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();


            do {
                times++;

                try {
                    response = httpClient.execute(httpPost, context);
                    resEntity = response.getEntity();
                    result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式

                    if(StringUtils.isNotBlank(result)){
                        JSONObject resultJson = JSON.parseObject(result);
                        String code  = resultJson.getString("code");
                        String data = resultJson.getString("data");

                        if("0".equalsIgnoreCase(code) && StringUtils.isNotBlank(data)){
                            break;
                        }
                    }
                } catch (Exception e) {
                   logger.error("doPostToResumeDownload 调用接口发生异常：" + e.getMessage(),e);
                }



                if(times > 3){
                    break;
                }
            } while (true);


        } catch (Exception e) {
            logger.error(String.format("doPostToResumeDownload 发生异常：%s",e));
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpPost.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return result;
    }






    //https://rd5.zhaopin.com/api/rd/resume/detail?_=1539136606956&resumeNo=4mHjlEZA4HH1h0dkNS0KLA_1_1%3B5055707C0AF8A9935F99F7EBE5FF2C1C%3B1539135281200
    public static String getResumeDetailForBuy(HttpClientContext context,Map<String,String> map){

        String url = "https://rd5.zhaopin.com/api/rd/resume/detail?_="+new Date().getTime()
                                                         +"&resumeNo=" + map.get("resumeNo");


        String result = "";
        HttpEntity resEntity = null;
        CloseableHttpResponse response = null;
        HttpGet httpGet = null;
        int times = 0;
        try {
            httpGet = new HttpGet(url);

            httpGet.setHeader("Proxy-Authorization", HttpClientUtil.getAuthHeader());
            httpGet.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            //httpPost.setHeader("Accept-Encoding","gzip, deflate, br");
            httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
            httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");

            httpGet.setHeader("Referer", map.get("Referer"));
            httpGet.setHeader("Host", "rd5.zhaopin.com");
            httpGet.setHeader("Cookie", map.get("Cookie"));

            CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

            do {

                times++;

                try {
                    response = httpClient.execute(httpGet, context);
                    resEntity = response.getEntity();
                    result = EntityUtils.toString(resEntity, "UTF-8");// 返回json格式：

                    if(StringUtils.isNotBlank(result)){
                        JSONObject resultJson = JSON.parseObject(result);
                        String code  = resultJson.getString("code");
                        String data = resultJson.getString("data");

                        if("0".equalsIgnoreCase(code) && StringUtils.isNotBlank(data)){
                            break;
                        }
                    }

                } catch (Exception e) {
                    logger.error("getResumeDetailForBuy 调用接口发生异常：" + e.getMessage(),e);
                }

                if(times > 6){
                    break;
                }
            } while (true);


        } catch (Exception e) {
            logger.error(String.format("getResumeDetailForBuy 发生异常：%s",e),e);
        }finally {
            try {
                IOUtils.closeQuietly(response);
                httpGet.releaseConnection();
                EntityUtils.consume(resEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return  result;


    }


    /****************************************购买简历相关接口-end********************************************/





}
